package edu.scu.declaresys.web.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.Configs;
import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.IProjectService;
import edu.scu.declaresys.service.intr.IUserService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.util.Text;
import edu.scu.declaresys.vo.Area;
import edu.scu.declaresys.vo.Project;
import edu.scu.declaresys.vo.ProjectClass;
import edu.scu.declaresys.vo.User;
import edu.scu.declaresys.web.ABaseAction;
import edu.scu.declaresys.web.util.ProjectUtils;
import edu.scu.declaresys.web.util.SessionUtil;

/**
 * 项目申报Action
 * @author: zt
 * @since : 2013-7-9
 */
public class DeclareProjectAction extends ABaseAction implements
		ModelDriven<Project>
{
	private static final long	serialVersionUID	= -7644636922967838191L;

	private IProjectService proService = ServiceFactory.createProjectService();
	private IUserService userService = ServiceFactory.createUserService();
	
	private Project project = new Project();
	private Integer page;
	private Integer projectId;
	
	//
	// actions.
	//

	public String declareWriteProject() throws Exception
	{
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	public String declareCommitProject() throws Exception
	{
		/**
		 *  Check null or empty.
		 */
		if(ProjectUtils.checkRequiredField(project) == false) 
		{
			throw new Exception("必填项未填写");
		}
		/**
		 * Check value valid or not.
		 */
		ProjectUtils.doLogicalValidation(project);
		/**
		 * set values.
		 */
		User user = SessionUtil.getCurrentUser(session);
		// set level and area id.
		project.setAreaCountyId(user.getCountyId());
		project.setAreaCityId(user.getCityId());
		// set user id.
		project.setDeclareUserId(user.getId());
		project.setCountyRecUserId(User.NULL_USER_ID);
		project.setCityRecUserId(User.NULL_USER_ID);
		project.setProvinceRecUserId(User.PROVINCE_RCMD_USER_ID);
		project.setAuditUserId(User.PROVINCE_AUDIT_USER_ID);
		// Set state. and recommend user.
		PageList<User> rcmdUsers = userService.findUserList(null, user.getCountyId(), null, User.ROLE_PROJECT_RECOMMEND, null);
		if(rcmdUsers.size() == 1)
		{
			project.setCountyRecUserId(rcmdUsers.get(0).getId());
		}
		rcmdUsers = userService.findUserList(user.getCityId(), Area.NULL_AREA_ID, null, User.ROLE_PROJECT_RECOMMEND, null);
		if(rcmdUsers.size() == 1)
		{
			project.setCityRecUserId(rcmdUsers.get(0).getId());
		}
		//
		if(user.getLevel().equals(Area.LEVEL_COUNTY))
		{
			project.setState(Project.STATE_UNRCMD_COUNTY);
		}
		else if(user.getLevel().equals(Area.LEVEL_CITY))
		{
			project.setState(Project.STATE_UNRCMD_CITY);
		}
		else
		{
			throw new Exception("省级用户不能申报项目");
		}
		project.setLevel(user.getLevel());
		project.setUnitName(user.getUnitName());
		project.setYear(Calendar.getInstance().get(Calendar.YEAR));
		
		// Set total fund.
		project.setTotalFunds(project.getGrantFunds() + project.getOtherFunds() + project.getSelfRaisedFunds());
		if(Text.isNullOrEmpty(project.getCountyRecFileno()))
		{
			project.setCountyRecFileno("");
		}
		if(Text.isNullOrEmpty(project.getCityRecFileno()))
		{
			project.setCityRecFileno("");
		}
		//
		// Set time.
		Date crtDate = new Date();
		project.setCreateTime(crtDate);
		project.setCommitTime(crtDate);
		project.setCountyRecTime(Configs.defaultDate);
		project.setCityRecTime(Configs.defaultDate);
		project.setProvinceRecTime(Configs.defaultDate);
		project.setAuditTime(Configs.defaultDate);
		//
		Integer id = proService.addProject(project);
		//
		request.setAttribute(Project.T_ID, id);
		return Action.SUCCESS;
	}
	
	public String declareRequestModifyMyProject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		Project oldProject = proService.findById(project.getId(), Depth.ZERO);
		if(oldProject.getState().equals(Project.STATE_PASSED)
				|| oldProject.getState().equals(Project.STATE_REJECTED))
		{
			throw new Exception("该项目已经不能修改");
		}
		project.setState(Project.STATE_REQUEST_MODIFY);
		proService.update(project);
		return Action.SUCCESS;
	}
	
	public String declareGetMyCommitedProject() throws Exception
	{
		if(page == null){
			page = 1;
		}
		PageList<Project> voLst =  proService.getMyCommittedProject(SessionUtil.getCrtUserId(session),
					project.getProjectClassId(), project.getState(), project.getName(),
					page, Project.T_ID, true);
		List<ProjectClass> projectClassLst = ServiceFactory.createProjectClassService().findClassList();
	
		request.setAttribute(IAttrName.CLASS_LIST, projectClassLst);
		request.setAttribute(IAttrName.PROJECT_LIST, voLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, voLst.getDataAmt());
		return Action.SUCCESS;
	}
	
	//
	// Getters and Setters.
	//
	@Override
	public Project getModel()
	{
		return project;
	}

	public Project getProject()
	{
		return project;
	}

	public void setProject(Project project)
	{
		this.project = project;
	}

	public IProjectService getProService()
	{
		return proService;
	}

	public void setProService(IProjectService proService)
	{
		this.proService = proService;
	}

	public Integer getPage()
	{
		return page;
	}

	public void setPage(Integer page)
	{
		this.page = page;
	}

	public Integer getProjectId()
	{
		return projectId;
	}

	public void setProjectId(Integer projectId)
	{
		this.projectId = projectId;
	}

	
}
